@{
    // odd formatting in this file is to cause more attractive results in the output.
    // 一级菜单的所有下级菜单
    var items = Enumerable.Cast<dynamic>((System.Collections.IEnumerable)Model);
}

@{
    @*菜单的文本不存在*@
    if (!HasText(Model.Text)) 
    {
        @DisplayChildren(Model)
    }
    else 
    {
        if ((bool) Model.Selected) 
        {
            Model.Classes.Add("active");
        }

        @* morphing the shape to keep Model untouched*@
        Model.Metadata.Alternates.Clear();
        @*带有下级*@
        if (items.Any()) 
        {
            Model.Metadata.Type = "MenuItemMain";      
        }
        else
        {
            Model.Metadata.Type = "MenuItemLink";
        }
        @* render the menu item only if it has some content *@
        var renderedMenuItemLink = Display(Model);
        if (HasText(renderedMenuItemLink)) {
            var tag = Tag(Model, "li");
            @tag.StartElement
            @renderedMenuItemLink
            // 如果还有下级菜单递归下级菜单
            if (items.Any()) {
                <ul class="sub-menu">
                    @DisplayChildren(Model)
                </ul>
            }    
            @tag.EndElement
        }
    }
}
